package com.qijian.quality.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.quality.domain.InspectionResultRecord;
import com.qijian.quality.domain.query.InspectionResultRecordQuery;
import com.qijian.quality.domain.vo.InspectionResultRecordDetailVo;
import com.qijian.quality.domain.vo.InspectionResultRecordVo;
import com.qijian.quality.service.IInspectionResultRecordService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 质检结果记录Controller
 *
 * @author qijian
 * @date 2025-03-28
 */
@Api(tags = "质检结果记录")
@RestController
@RequiredArgsConstructor
@RequestMapping("/quality/inspectionResultRecord")
public class InspectionResultRecordController extends BaseController {

    private final IInspectionResultRecordService inspectionResultRecordService;

    /**
     * 查询质检结果记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(InspectionResultRecordQuery query, Page<InspectionResultRecord> page) {
        return getDataTable(inspectionResultRecordService.pageVo(page, WhereEntityTool.invoke(query)));
    }

    /**
     * 质检数据
     * @param id
     * @return
     */
    @GetMapping("/inspectionData/{id}")
    public AjaxResult inspectionData(@PathVariable Long id) {
        List<InspectionResultRecordDetailVo> list = inspectionResultRecordService.getInspectionData(id);
        return AjaxResult.data(list);
    }



    /**
     * 导出质检结果记录列表
     */
    @Log(title = "质检结果记录" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(InspectionResultRecordQuery query, HttpServletResponse response) {
        new ExcelUtil<>(InspectionResultRecord. class).exportExcel(response,inspectionResultRecordService.list( WhereEntityTool.invoke(query)), "质检结果记录数据");
    }

    /**
     * 获取质检结果记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(inspectionResultRecordService.getById(id));
    }

    /**
     * 新增质检结果记录
     */
    @Log(title = "质检结果记录" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectionResultRecord inspectionResultRecord) {
        return toAjax(inspectionResultRecordService.save(inspectionResultRecord));
    }

    /**
     * 修改质检结果记录
     */
    @Log(title = "质检结果记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectionResultRecord inspectionResultRecord) {
        approval(inspectionResultRecordService.getById(inspectionResultRecord.getId()));
        return toAjax(inspectionResultRecordService.updateById(inspectionResultRecord));
    }

    /**
     * 删除质检结果记录
     */
    @Log(title = "质检结果记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        ids.forEach(id -> approval(inspectionResultRecordService.getById(id)));
        return toAjax(inspectionResultRecordService.removeByIds(ids));
    }


    /**
     * 海达巡检功能
     * @param inspectionResultRecordVo
     * @return
     */
    @PostMapping("/savePatrolInspection")
    public AjaxResult savePatrolInspection(@RequestBody InspectionResultRecordVo inspectionResultRecordVo) {
        return toAjax(inspectionResultRecordService.savePatrolInspection(inspectionResultRecordVo));
    }
}
